import { createRouter, createWebHistory } from "vue-router";
import routerConfig from "./RouterConfig.ts";
import { useUserStore } from "../stores/useUserStore.js";
import { useRouteStore } from "../stores/useRouteStore.js";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/Login.vue"),
    },

    //首页
    {
      path: "/home",
      name: "home",
      component: () => import("../views/HomeView.vue"),
      children: [
        //主页
        {
          path: "control",
          name: "control",
          component: () => import("../views/homePage/ControlView.vue"),
          meta: {
            name: "控制台",
          },
        },
        {
          path: "ArticleCheck",
          name: "ArticleCheck",
          component: () =>
            import("../views/ArticleManagement/ArticleCheckView.vue"),
          meta: {
            name: "文章审核",
          },
        },


        // kz 视频审核
        {
          path: "videoCheck",
          name: "videoCheck",
          component: () =>
            import("../views/videoManagement/videoManage.vue"),
          meta: {
            name: "文章审核",
          },
        },
        {
          path: "videoShow",
          name: "videoShow",
          component: () =>
            import("../views/videoManagement/videoShow.vue"),
          meta: {
            name: "文章审核",
          },
        },
        //权限
        {
          path: "admin",
          name: "admin",
          component: () => import("../views/ums/AdminView.vue"),
        },
        {
          path: "role",
          component: () => import("../views/ums/RoleView.vue"),
          meta: {
            title: "角色列表",
          },
        },
        {
          path: "roleEdit/:id",
          name: "roleEdit",
          component: () => import("../views/ums/RoleEditView.vue"),
          meta: {
            title: "角色编辑",
          },
        },

        //营销
        {
          path: "brand",
          component: () => import("../views/sms/BrandView.vue"),
          meta: {
            title: "课程管理",
          },
        },
        {
          path: "flash",
          component: () => import("../views/sms/FlashView.vue"),
          meta: {
            title: "秒杀活动列表",
          },
        },
      ],
    },
  ],
});
//vue3.0路由守卫
router.beforeEach((to, from, next) => {
  const token = sessionStorage.getItem("token");
  if (to.path == "/login") {
    next();
  } else {
    if (token) {
      next();
    } else {
      next("/login");
    }
  }
});

//路由守卫
// router.beforeEach((to,from,next)=>{
//   const user=useUserStore();
//   const getRouter=useRouteStore();
//   if(to.path=="/login"){
//     next();
//   }else{
//     if(){

//     }
//   }

// })

// const ConfigRouter = () => {
//   const getRoute = useRouteStore();
//   router.addRoute("home", {
//     path: "/home",
//     name: "home",
//     component: () => import("../views/HomeView.vue"),
//   });
//   router.addRoute("home", routerConfig[0]);
//   routerConfig.forEach((item) => {
//     checkRermission(item.url) && router.addRoute("home", item);
//   });
//   getRoute.isGetterRouter = true;
// };

// const checkRermission = (path) => {
//   const user = useUserStore;
//   return user;
// };

export default router;
